import Ouerchanin from '@/views/Ouerchanin.vue'
import Layout from '@/layout'

export function getRoutes (list) {//input list  //递归，为返回的字符串添加组件

  let routes = fn(list)
  routes = routes.map(obj => {
    obj.path = obj.path.indexOf('/') > -1 ? obj.path : '/' + obj.path
    obj.component = Layout
    // console.log(obj)
    if (obj.children && obj.children.length > 0) {
      // obj['redirect']
    } else {
      obj['redirect'] = `${obj.path}/${obj.name}index`
      obj['children'] = [{
        name: `${obj.name}index`,
        path: `${obj.name}index`,
        meta: { ...obj.meta },
        component: Ouerchanin,
      }]
    }
    return obj
  })
  // console.log(routes)
  return routes
}

function fn (list) {
  return list.map((obj) => {
    if (obj.name.indexOf('/') > -1) {
      obj.name = obj.name.replace('/', ' ')
    }
    if (obj.name.indexOf('(') > -1) {
      obj.name = obj.name.replace('(', '（')
    }
    if (obj.name.indexOf(')') > -1) {
      obj.name = obj.name.replace(')', '）')
    }
    // if (obj.hide === null) obj.hide = true//这句是在测试,router->hidden->true->隐藏
    let hidden = obj.hide === 1
    if (obj.children) {
      return {
        name: obj.name,
        path: obj.name,
        component: Ouerchanin,
        hidden,
        meta: { icon: obj.icon, title: obj.name, url: obj.path },
        children: fn(obj.children),
      }
    } else {
      return {
        name: obj.name,
        path: obj.path,
        component: Ouerchanin,
        hidden,
        meta: { icon: obj.icon, title: obj.name, url: obj.path },
        children: [],
      }
    }
  })
}
